package ghostcat.parse.display {
	import flash.display.*;
	import flash.geom.*;
	import ghostcat.debug.*;
	import ghostcat.parse.*;

	public class DrawParse extends DisplayParse {
		public var fillColor:uint = 16777215;
		public var matrix:Matrix = null;
		public var smoothing:Boolean = false;
		public var colorTransform:ColorTransform = null;
		public var source:IBitmapDrawable;
		public var clipRect:Rectangle = null;
		public var transparent:Boolean = true;
		public var blendMode:String = null;

		public function DrawParse(param1:IBitmapDrawable, param2:Matrix = null, param3:ColorTransform = null, param4:Rectangle = null, param5:Boolean = false, param6:Boolean = true, param7:uint = 16777215) {
			this.source = param1;
			this.matrix = param2;
			this.colorTransform = param3;
			this.blendMode = blendMode;
			this.clipRect = param4;
			this.smoothing = param5;
			this.transparent = param6;
			this.fillColor = param7;
			return;
		} // end function

		override public function parseBitmapData(param1:BitmapData):void {
			var target:* = param1;
			super.parseBitmapData(target);
			try {
				target.draw(source, matrix, colorTransform, blendMode, clipRect, smoothing);
			} catch (e:Error) {
			}
			return;
		} // end function

		public function createBitmap():Bitmap {
			var bitmap:Bitmap;
			var displayObj:* = source as DisplayObject;
			if (!displayObj) {
				return null;
			}
			var bounds:* = displayObj.getBounds(displayObj);
			var width:* = Math.ceil(bounds.width);
			var height:* = Math.ceil(bounds.height);
			if (!matrix) {
				matrix = new Matrix();
				matrix.tx = matrix.tx - bounds.x;
				matrix.ty = matrix.ty - bounds.y;
			}
			try {
				bitmap = new Bitmap(new BitmapData(width, height, transparent, fillColor));
			} catch (e:Error) {
				bitmap = new Bitmap();
			}
			if (source is DisplayObject) {
				bitmap.x = (source as DisplayObject).x + bounds.x;
				bitmap.y = (source as DisplayObject).y + bounds.y;
			}
			this.parse(bitmap);
			return bitmap;
		} // end function

		public function createBitmapData():BitmapData {
			return createBitmap().bitmapData;
		} // end function

		override public function parse(param1):void {
			if (param1 is Bitmap) {
				param1 = (param1 as Bitmap).bitmapData;
			}
			super.parse(param1);
			return;
		} // end function

		public static function createBitmap(param1:IBitmapDrawable, param2:Matrix = null, param3:ColorTransform = null, param4:Rectangle = null, param5:Boolean = false):Bitmap {
			return new DrawParse(param1, param2, param3, param4, param5).createBitmap();
		} // end function

	}
}
